﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule.Contracts.Municipios;
using Cesave.Domain.PermisosModule.Entities;
using Cesave.Infrastructure.CrossCutting.Logging;
using Cesave.Infrastructure.Data.PermisosModule.Context;
using Cesave.Infrastructure.Data.PermisosModule.Repositories;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Cesave.Infrastructure.Data.PermisosModule.Tests.RepositoriesTests
{
	[TestClass]
	public class MunicipioRepositoryTests
		: GenericRepositoryTestsBase<Municipio>
	{
		/// <summary>
		/// 
		/// </summary>
		public override System.Linq.Expressions.Expression<Func<Municipio, bool>> FilterExpression
		{
			get
			{
				return municipio => municipio.MunicipioId == 42;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public override System.Linq.Expressions.Expression<Func<Municipio, int>> OrderByExpression
		{
			get
			{
				return municipio => municipio.MunicipioId;
			}
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void FindMunicipiosByEstadoSpecification_Null_Specification_Throws_ArgumentNullException()
		{
			// Arrange
			IPermisosModuleContext context = GetContext();
			ITraceManager traceManager = GetTraceManager();
			IMunicipioRepository repository = new MunicipioRepository(context, traceManager);

			MunicipiosByEstadoSpecification specification = null;

			// Act
			var actual = repository.FindMunicipiosByEstadoSpecification(specification);
		}

		[TestMethod]
		[ExpectedException(typeof(InvalidOperationException))]
		public void FindMunicipiosByEstadoSpecification_Empty_Guid_Throws_InvalidOperationException()
		{
			// Arrange
			IPermisosModuleContext context = GetContext();
			ITraceManager traceManager = GetTraceManager();
			IMunicipioRepository repository = new MunicipioRepository(context, traceManager);

			//Guid estadoGuid = new Guid("B13F5C3F-AA42-4943-A8E9-49BA546EE5D1");
			Guid estadoGuid = Guid.Empty;
			MunicipiosByEstadoSpecification specification = new MunicipiosByEstadoSpecification(estadoGuid);

			// Act
			var actual = repository.FindMunicipiosByEstadoSpecification(specification);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void FindMunicipiosByEstadoSpecificationAndStatus_Null_Specification_Throws_ArgumentNullException()
		{
			// Arrange
			IPermisosModuleContext context = GetContext();
			ITraceManager traceManager = GetTraceManager();
			IMunicipioRepository repository = new MunicipioRepository(context, traceManager);

			MunicipiosByEstadoSpecification specification = null;

			// Act
			var actual = repository.FindMunicipiosByEstadoSpecificationAndStatus(specification);
		}

		[TestMethod]
		[ExpectedException(typeof(InvalidOperationException))]
		public void FindMunicipiosByEstadoSpecificationAndStatus_Empty_Guid_Throws_InvalidOperationException()
		{
			// Arrange
			IPermisosModuleContext context = GetContext();
			ITraceManager traceManager = GetTraceManager();
			IMunicipioRepository repository = new MunicipioRepository(context, traceManager);

			//Guid estadoGuid = new Guid("B13F5C3F-AA42-4943-A8E9-49BA546EE5D1");
			Guid estadoGuid = Guid.Empty;
			MunicipiosByEstadoSpecification specification = new MunicipiosByEstadoSpecification(estadoGuid);

			// Act
			var actual = repository.FindMunicipiosByEstadoSpecificationAndStatus(specification);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void FindMunicipiosByDistritoSpecification_Null_Specification_Throws_ArgumentNullException()
		{
			// Arrange
			IPermisosModuleContext context = GetContext();
			ITraceManager traceManager = GetTraceManager();
			IMunicipioRepository repository = new MunicipioRepository(context, traceManager);

			MunicipiosByDistritoSpecification specification = null;

			// Act
			var actual = repository.FindMunicipiosByDistritoSpecification(specification);
		}

		[TestMethod]
		[ExpectedException(typeof(InvalidOperationException))]
		public void FindMunicipiosByDistritoSpecification_Empty_Guid_Throws_InvalidOperationException()
		{
			// Arrange
			IPermisosModuleContext context = GetContext();
			ITraceManager traceManager = GetTraceManager();
			IMunicipioRepository repository = new MunicipioRepository(context, traceManager);

			MunicipiosByDistritoSpecification specification = new MunicipiosByDistritoSpecification(Guid.Empty);

			// Act
			var actual = repository.FindMunicipiosByDistritoSpecification(specification);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void FindMunicipiosByDistritoSpecificationAndStatus_Null_Specification_Throws_ArgumentNullException()
		{
			// Arrange
			IPermisosModuleContext context = GetContext();
			ITraceManager traceManager = GetTraceManager();
			IMunicipioRepository repository = new MunicipioRepository(context, traceManager);

			MunicipiosByDistritoSpecification specification = null;

			// Act
			var actual = repository.FindMunicipiosByDistritoSpecificationAndStatus(specification);
		}

		[TestMethod]
		[ExpectedException(typeof(InvalidOperationException))]
		public void FindMunicipiosByDistritoSpecificationAndStatus_Empty_Guid_Throws_InvalidOperationException()
		{
			// Arrange
			IPermisosModuleContext context = GetContext();
			ITraceManager traceManager = GetTraceManager();
			IMunicipioRepository repository = new MunicipioRepository(context, traceManager);

			MunicipiosByDistritoSpecification specification = new MunicipiosByDistritoSpecification(Guid.Empty);

			// Act
			var actual = repository.FindMunicipiosByDistritoSpecificationAndStatus(specification);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void FindMunicipioByGuidSpecification_Null_Specification_Throws_ArgumentNullException()
		{
			// Arrange
			IPermisosModuleContext context = GetContext();
			ITraceManager traceManager = GetTraceManager();
			IMunicipioRepository repository = new MunicipioRepository(context, traceManager);

			MunicipioByGuidSpecification specification = null;

			// Act
			var actual = repository.FindMunicipioByGuidSpecification(specification);
		}

		[TestMethod]
		[ExpectedException(typeof(InvalidOperationException))]
		public void FindMunicipioByGuidSpecification_Empty_Guid_Trhows_InvalidOperationException()
		{
			// Arrange
			IPermisosModuleContext context = GetContext();
			ITraceManager traceManager = GetTraceManager();
			IMunicipioRepository repository = new MunicipioRepository(context, traceManager);

			MunicipioByGuidSpecification specification = new MunicipioByGuidSpecification(Guid.Empty);

			// Act
			var actual = repository.FindMunicipioByGuidSpecification(specification);
		}

		[TestMethod]
		public void FindMunicipiosByEstadoSpecification_Invoke()
		{
			// Arrange
			IPermisosModuleContext context = GetContext();
			ITraceManager traceManager = GetTraceManager();
			IMunicipioRepository repository = new MunicipioRepository(context, traceManager);
			
			Guid estadoGuid = new Guid("B13F5C3F-AA42-4943-A8E9-49BA546EE5D1");
			MunicipiosByEstadoSpecification specification = new MunicipiosByEstadoSpecification(estadoGuid);

			// Act
			var actual = repository.FindMunicipiosByEstadoSpecification(specification);

			// Assert
			Assert.IsNotNull(actual);
			actual.ToList().ForEach(municipio =>
			{
				Assert.AreEqual(estadoGuid, municipio.EstadoGuid);
			});
		}

	}
}
